﻿$(document).ready(function(){
	$(".disable").click(function(e){
		return nothing(e);
	});
});

$(document).ready(function(){
	if($('#more_button').data('since')&&$('#more_button').data('since').length){
		$('#more_button').parent().show();
		$('#more_button').click(function(e){
			basicMoreButtonHandler(this,'#page_container');
			return nothing(e);
		});
	}
});

function basicMoreButtonHandler(caller,target){
	if(!$(target)||!$(caller))return;
	if($(caller).children('*:visible').length==0)
		return;
	$.ajax({
		url:$(caller).data('since'),
		beforeSend:function(){
			$(caller).addClass("loading").children().hide();
		},
		success:function(html){
			$(target).append(html);
			if($('#more_button').data('since').length==0)
				$('#more_button').parent().hide();
			else{
				$('#more_button').parent().show();
				$('#more_button').removeClass("loading").children().show();
			}
			ajaxLoaded();
		}
	});
}

$(document).ready(function(){
	if($('#navi-col').attr('navimenu')){
		$('a.'+$('#navi-col').attr('navimenu')).addClass('selected');
	}
	if($('#navi-col').attr('navicol')){
		$('.navi-'+$('#navi-col').attr('navicol')+' a').addClass('selected');
	}
});

function dropMenuShow(){
	$('#GNB div.content').addClass('selected');
	$('#menuBtn .arrow').addClass('open');
	$('#dropMenu').addClass('visible').show();
	$(document).click(function(e){
		dropMenuHide();
		$(this).unbind('click');
	});
}

function dropMenuHide(){
	$('#GNB div.content').removeClass('selected');
	$('#menuBtn .arrow').removeClass('open');
	$('#dropMenu').removeClass('visible').hide();
}

$(document).ready(function(){
	$('#GNB div.box').click(function(e){
		if($('#dropMenu').hasClass('visible')){
			dropMenuHide();
		}else{
			dropMenuShow();
		}
		nothing(e);
	});
});

function ajaxLoaded(){
	$('.copy_more').unbind('click').click(function(){
		if($(this).hasClass('expanded')){
			$(this).removeClass('expanded');
			$('#'+$(this).attr('target')).slideUp('fast');
		}else{
			$(this).addClass('expanded');
			$('#'+$(this).attr('target')).slideDown('fast');
		}
	});
}


$(document).ready(function(){
	ajaxLoaded();
});

$(document).ready(function(){
	$('.message').each(function(){
		$('#messageContainer:hidden').fadeIn('fast').delay(3000).fadeOut('slow');
	});
});

$(document).ready(function(){
	$('.message .dismiss').click(function(){
		$('#messageContainer:visible').stop().fadeOut('fast');
	});
});

$(document).ready(function(){
	$('#footer .country').click(function(e){
		popupHandler.get().load({
			url:$(this).attr('href'),
			offsetParent:$('#footer'),
			horizon:'center',
			vertical:'bottom'
		});
		nothing(e);
		return false;
	});
});

$(document).ready(function(){
	$('.adSpentGraphCnt').hover(function(){
		$('.cloudCnt',this).fadeIn();
	},function(){
		$('.cloudCnt',this).hide();
	});
});

function nothing(e){
	e.stopPropagation();
	e.preventDefault();
	return false;
};

function isNumeric(n){
	return!isNaN(parseFloat(n))&&isFinite(n);
}


function getNumeric(num){
	return isNumeric(num)?parseFloat(num):0;
}


function makeErrorMessage(message){
	var msgContainer = $('#messageContainer');
	var msgLeft=$('body').width()/3+"px";
	var msgTop=$('body').height()/3+"px";
	msgContainer.css({left:msgLeft,top:msgTop});
	$('#messageContainer').dequeue();
	$('#messageContainer').html('<div class="container"><div class="message"><div class="title"><h4 class="bold">错误提示</h4></div><div class="error">'+message+'</div></div></div>').fadeIn('fast').delay(1000).fadeOut('slow');
}


var popupHandler=(function(){
	var instantiated;
	function init(){
		var offsetParent=$('body');
		var popupParent=$('body');
		var popupContainer=null;
		function _confirmBtnHandler(btnConfirm){
			if($(btnConfirm).attr('href')){
				$.ajax({
					url:$(btnConfirm).attr('href'),
					beforeSend:function(){
						$('.footSection .confirm').hide();
						$('.footSection .loading').show();
					},
					success:function(data){
						if(data.message){
							makeErrorMessage(data.message);
						}
						if(data.reload){
							location.reload();
						}
						_remove();
					}
				});
			}
		}
		function _setOffset(args){
			offset=offsetParent.offset();
			if(args.horizon=='center'){
				popupLeft=offset.left+(offsetParent.width()-popupContainer.width())/2;
			}else if(args.horizon=='right'){
				popupLeft=offset.left+(offsetParent.width()-popupContainer.width());
			}else{
				popupLeft=offset.left;
			}
			if(args.vertical=='middle'){
				popupTop=offset.top+(offsetParent.height()-popupContainer.height())/2;
			}else if(args.vertical=='bottom'){
				popupTop=offset.top+(offsetParent.height()-popupContainer.height());
			}else{
				popupTop=offset.top;
			}
			popupContainer.offset({
				left:popupLeft,top:popupTop
			});
		}
		function _load(args){
			if(popupContainer==null){
				popupContainer=$("<div/>",{"class":"popupContainer","id":"popupContainer"}).appendTo(popupParent);
			}
			if(args.offsetParent){
				obj=$(args.offsetParent);
				if(obj){
					offsetParent=args.offsetParent;
				}
				delete obj;
			}
			if(args.url){
				$.ajax({
					url:args.url,
					success:function(data){
						popupContainer.html(data);
						$('a.popupClose').click(function(){
							_remove();
							return false;
						});
						$('a.confirm').click(function(){
							_confirmBtnHandler($(this));
							return false;
						});
						if(args.callback){
							args.callback(data);
						}
						if(args.offsetParent){
							_setOffset(args);
						}
					}
				});
			}
		}
		function _remove(){
			popupContainer.remove();
			popupContainer=null;
		}
		return{
			load:function(args){
				_load(args);
			},
			remove:function(){
				_remove();
			}
		}
	}
	return{
		get:function(){
			if(!instantiated){
				instantiated=init();
			}
			return instantiated;
		}
	}
})();